package com.dave.admin.config.auth.component;

import com.dave.admin.modular.boss.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 自定义权限认证方式
 * <p>
 * TODO 优化看request 能不能拿到 可以记录下请求方法
 *
 * @author Dave
 */
@Slf4j
@Component
public class BossPermissionEvaluator implements PermissionEvaluator {


    @Override
    public boolean hasPermission(Authentication authentication, Object o, Object o1) {
        String permissionPrefix = ((BaseController) o).getPermissionPrefix() + o1;
        if (authentication.getAuthorities().stream().anyMatch(x -> x.getAuthority().equals(permissionPrefix))) {
            return true;
        }
        log.info("当前用户【{}】访问方法必须需要有该权限值:{}", authentication.getName(), permissionPrefix);
        return false;
    }

    @Override
    public boolean hasPermission(Authentication authentication, Serializable serializable, String s, Object o) {
        return false;
    }
}
